# -*- coding: utf-8 -*-
import os
from django.conf import settings
from django.template import loader, RequestContext
from django.http import HttpResponse

BASE_CONTEXT = {
        'title': 'Hyperlog',
        }

def load_local_template(template_name):
    """
    Returns a compiled Template object for the given template name,
    handling template inheritance recursively.
    """
    filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                'templates',template_name)
    source = open(filepath).read().decode(settings.FILE_CHARSET)
    template = loader.get_template_from_string(source, name=template_name)
    return template

def render_local_template_to_response(request, template_name, extra_context=None):
    context = RequestContext(request, BASE_CONTEXT)
    if extra_context:
        context.update(extra_context)
    return HttpResponse(load_local_template(template_name).render(context))
